## How do people evaluate foreign aid to “nasty” regimes?
## Tobias Heinrich & Yoshiharu Kobayashi
## British Journal of Political Science
#########################################################

## Generate two vectors of weights via entropy balancing

## Setup
########
## Load data
load("output/Modified MTurk Data.Rdata")
load("output/Modified CCES Data.Rdata")

## Formulas for the balancing
formulas <- c(as.formula(~ 1 + Age + I(Gender == 2) + I(EconChange %in% c(1,2)) + RepDem + I(Education %in% c(5,6))),
              as.formula(~ 1 + Age + I(Gender == 2) + RepDem + I(Education %in% c(5,6))),
              as.formula(~ 1 + Age + I(Gender == 2)))
              

## Run Ebalance
###############
## First element is for full data set, second for data set without "addressing" aid
weight_vec <- list("list", 2)
balance_data <- c()
for(j in 1:2)
{
  if(j == 1) weight_vec[[j]] <- matrix(NA, nrow(data), length(formulas))
  if(j == 2) weight_vec[[j]] <- matrix(NA, nrow(subset(data, RA == 0)), length(formulas))
  
  for(i in 1:length(formulas))
  {
    ## Get balance
    this_form <- formulas[i][[1]]
    if(j == 1) mm_data <- model.matrix(frml=this_form, data=data)[, -1]
    if(j == 2) mm_data <- model.matrix(frml=this_form, data=subset(data, RA == 0))[, -1]
    mm_cces <- model.matrix(frml=this_form , data=cces)[, -1]
    ebal_data <- rbind(mm_data, mm_cces)
    ebal_treat <- c(rep(0, nrow(mm_data)), rep(1, nrow(mm_cces)))
    ebal <- ebalance(Treatment=ebal_treat, X=ebal_data)
    weight_vec[[j]][, i] <- ebal$w
    
    ## Evaluate balance
    bal <- adply(.data=1:ncol(mm_data), .margins=1, 
                 .fun=function(x) balanceUV(Co=mm_data[,x], Tr=mm_cces[,x], ks=FALSE, 
                                            weights.Co=ebal$w, estimand="ATC")$sdiff/100)
    bal <- rbind(bal,
                 adply(.data=1:ncol(mm_data), .margins=1, 
                       .fun=function(x) balanceUV(Co=mm_data[,x], Tr=mm_cces[,x], ks=FALSE, 
                                                  estimand="ATC")$sdiff/100))
    bal$What <- rep(c("eBalance", "Raw"), each=ncol(mm_data))
    bal$Name <- rep(colnames(mm_data), 2)
    bal$Formula <- "Complex"
    bal$Set <- j
    if(i == 2) bal$Formula <- "Basic + demographics"  
    if(i == 3) bal$Formula <- "Basic"
    
    balance_data <- rbind(balance_data, bal)
  }
}
save(weight_vec, file="output/Weight vectors.Rdata")


## Graph balance
###################
## Prettify variable names
balance_data$PrettyName <- ""
balance_data$PrettyName[balance_data$Name == "RepDem"] <- "Republican/ Democrat"
balance_data$PrettyName[balance_data$Name == "Age"] <- "Age"
balance_data$PrettyName[balance_data$Name == "I(Gender == 2)TRUE"] <- "Female"
balance_data$PrettyName[balance_data$Name == "I(Age * I(Gender == 2))"] <- "Female/ Age interaction"
balance_data$PrettyName[balance_data$Name == "I(EconChange %in% c(1, 2))TRUE"] <- "Life got (much) worse"
balance_data$PrettyName[balance_data$Name == "I(EconChange %in% c(4, 5))TRUE"] <- "Life got (much) better"
balance_data$PrettyName[balance_data$Name == "I(Occupation == 1)TRUE"] <- "Full-time job"
balance_data$PrettyName[balance_data$Name == "I(Education %in% c(5, 6))TRUE"] <- "College or more"
balance_data$PrettyName <- factor(balance_data$PrettyName, 
                                  levels=rev(c("Age", "Female", "Female/ Age interaction",
                                           "Republican/ Democrat", "Full-time job", "College or more",
                                           "Life got (much) worse", "Life got (much) better")))
balance_data$Dataset <- ifelse(balance_data$Set == 1, "Full data set", "Without remedial aid")
balance_data$Sample <- balance_data$What
balance_data$Formula <- factor(balance_data$Formula, level=c("Basic", "Basic + demographics", "Complex"))


g <- ggplot(data=balance_data, aes(x=PrettyName, y=V1, shape=Sample))
g <- g + geom_hline(yintercept=0, size=.41)
g <- g + facet_grid(Dataset ~ Formula)
g <- g + geom_point(size=3, colour="grey30") + xlab("") + ylab("Standardized differences in means")
g <- g + theme_bw() + coord_flip()
g <- g + theme(axis.text.y = element_text(hjust=0, size=rel(.9)),
               axis.text.x = element_text(hjust=0, size=rel(.9)),
               panel.grid.major.y = element_line(size=0.25, colour="grey80", 
                                                 linetype="dashed"),
               panel.grid.major = element_blank(),
               panel.grid.minor = element_blank(),
               axis.ticks = element_blank(),
               strip.text = element_text(size=rel(1.1), hjust=0),
               strip.background = element_blank(),
               plot.title = element_text(size=rel(1.8), hjust=0))
ggsave(file="output/figures/Balance.pdf", width=11, height=7)
